package com.kanshu.ksgb.fastread.doudou.module.personal.presenter;

import com.kanshu.ksgb.fastread.doudou.R;
import com.kanshu.ksgb.fastread.doudou.app.Xutils;
import com.kanshu.ksgb.fastread.doudou.app.constants.Constants;
import com.kanshu.ksgb.fastread.doudou.base.basemvp.BaseMvpPresenter;
import com.kanshu.ksgb.fastread.doudou.base.basemvp.IGenrialMvpView;
import com.kanshu.ksgb.fastread.doudou.common.net.INetCommCallback;
import com.kanshu.ksgb.fastread.doudou.common.net.bean.BaseResult;
import com.kanshu.ksgb.fastread.doudou.common.net.retrofit.RetrofitHelper;
import com.kanshu.ksgb.fastread.doudou.common.net.rx.BaseObserver;
import com.kanshu.ksgb.fastread.doudou.common.util.NetUtils;
import com.kanshu.ksgb.fastread.doudou.module.personal.bean.MakeMoneyConfig;
import com.kanshu.ksgb.fastread.doudou.module.personal.bean.ReadFeedbackBean;
import com.kanshu.ksgb.fastread.doudou.common.business.commonbean.SignInData;
import com.kanshu.ksgb.fastread.doudou.module.personal.bean.SignInResult;
import com.kanshu.ksgb.fastread.doudou.module.personal.bean.UserBean;
import com.kanshu.ksgb.fastread.doudou.module.personal.bean.UserInfo;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import io.reactivex.Observer;
import io.reactivex.disposables.Disposable;
import io.reactivex.subjects.Subject;

/**
 * Created by wcy on 2017\6\23
 */
public class PersonCenterPresenter extends BaseMvpPresenter<IGenrialMvpView<UserBean>> {
    PersonCenterModel mModel = new PersonCenterModel();
    PersonCenterService personCenterService;
    public PersonCenterPresenter(Subject<Integer> lifeCyclerSubject) {
        super(lifeCyclerSubject);
        RetrofitHelper helper = RetrofitHelper.getInstance();
        personCenterService = helper.createService(PersonCenterService.class);
    }

    @Override
    public void detachView() {
        mModel.cancel();
        super.detachView();
    }

    public void getSignData(BaseObserver<SignInData> observer){
        personCenterService.getSignData()
                .compose(asyncRequest())
                .subscribe(observer);
    }

    public void doSign(int week, Observer<BaseResult<SignInResult>> observer){
        Map<String, String> map = new HashMap<>();
        map.put("week", String.valueOf(week));
        personCenterService.doSignIn(map)
                .compose(asyncRequest())
                .subscribe(observer);
    }

    public void getMakeMoneyConfig(INetCommCallback<MakeMoneyConfig> callback) {
        personCenterService.getMakeMoneyConfig().compose(asyncRequest()).subscribe(new BaseObserver<MakeMoneyConfig>() {
            @Override
            public void onResponse(BaseResult<MakeMoneyConfig> makeMoneyConfigBaseResult, MakeMoneyConfig makeMoneyConfig, Disposable disposable) {
                if (callback != null) {
                    callback.onResponse(makeMoneyConfig);
                }
            }

            @Override
            public void onError(int code, String errDesc) {
                super.onError(code, errDesc);
                if (callback != null) {
                    callback.onError(code, errDesc);
                }
            }
        });
    }

    public void checkPhone(String phone, String code, INetCommCallback<BaseResult<List<UserInfo>>> callback) {
        if (!NetUtils.isNetworkAvailable(Xutils.getContext())) {
            if (callback != null)
                callback.onError(Constants.ErrCode.NetErr,
                        Xutils.getContext().getString(R.string.standard_net_tip));
            return;
        }
        Map<String, String> map = new HashMap<>();
        map.put("phone", phone);
        map.put("code", code);
        personCenterService.checkPhone(map).compose(asyncRequest()).subscribe(new BaseObserver<List<UserInfo>>() {
            @Override
            public void onResponse(BaseResult<List<UserInfo>> result, List<UserInfo> list, Disposable disposable) {
                if (callback != null) {
                    callback.onResponse(result);
                }
            }

            @Override
            public void onError(int code, String errDesc) {
                super.onError(code, errDesc);
                if (callback != null) {
                    callback.onError(code, errDesc);
                }
            }
        });
    }

    public void changePhone(String phone, String code, INetCommCallback<BaseResult<List<UserInfo>>> callback) {
        if (!NetUtils.isNetworkAvailable(Xutils.getContext())) {
            if (callback != null)
                callback.onError(Constants.ErrCode.NetErr,
                        Xutils.getContext().getString(R.string.standard_net_tip));
            return;
        }
        Map<String, String> map = new HashMap<>();
        map.put("phone", phone);
        map.put("code", code);
        personCenterService.changePhone(map).compose(asyncRequest()).subscribe(new BaseObserver<List<UserInfo>>() {
            @Override
            public void onResponse(BaseResult<List<UserInfo>> result, List<UserInfo> list, Disposable disposable) {
                if (callback != null) {
                    callback.onResponse(result);
                }
            }

            @Override
            public void onError(int code, String errDesc) {
                super.onError(code, errDesc);
                if (callback != null) {
                    callback.onError(code, errDesc);
                }
            }
        });
    }

    public void checkReadFeedback(BaseObserver<ReadFeedbackBean> observer){
        personCenterService.checkReadFeedback()
                .compose(asyncRequest())
                .subscribe(observer);
    }


    public void userLogin(String phone, String code, String invitationCode, INetCommCallback<BaseResult<UserBean>> callback) {
        if (!NetUtils.isNetworkAvailable(Xutils.getContext())) {
            if (callback != null)
                callback.onError(Constants.ErrCode.NetErr,
                        Xutils.getContext().getString(R.string.standard_net_tip));
            return;
        }
        personCenterService.userLogin(phone, code, invitationCode).compose(asyncRequest()).subscribe(new BaseObserver<UserBean>() {
            @Override
            public void onResponse(BaseResult<UserBean> result, UserBean userBean, Disposable disposable) {
                if (callback != null) {
                    callback.onResponse(result);
                }
            }

            @Override
            public void onError(int code, String errDesc) {
                super.onError(code, errDesc);
                if (callback != null) {
                    callback.onError(code, errDesc);
                }
            }
        });
    }
}
